package org.example.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import org.example.domain.CarFile;
import org.example.service.CarFileService;

import java.util.List;

public class CarFileListener implements ReadListener<CarFile> {
    private static final int BATCH_COUNT = 3;
    private List<CarFile> fileList= ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private final CarFileService carFileService;
    public CarFileListener(CarFileService carFileService) {
        this.carFileService = carFileService;
    }
    @Override
    public void invoke(CarFile carFile, AnalysisContext analysisContext) {
        System.out.println("解析出来的数据"+carFile);
        fileList.add(carFile);
        if(fileList.size()>=BATCH_COUNT){
            System.out.println("list:"+fileList);
            carFileService.saveOrupdate(fileList);
            fileList=ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("解析完成");
        if(fileList.size()>1){
            carFileService.saveOrupdate(fileList);
            System.out.println("list"+fileList);
        }
    }
}
